<template>
  <el-dialog
    v-model="dialogFormVisible"
    :close-on-click-modal="false"
    :title="title"
    width="520px"
    @close="close"
  >
    <el-form ref="formRef" label-width="120px" :model="form" :rules="rules">
      <el-form-item label="链接名称：" prop="link_name">
        <el-input
          v-model.trim="form.link_name"
          placeholder="请输入链接名称"
          rows="4"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">创 建</el-button>
    </template>
  </el-dialog>
</template>

<script>
  import { doLinkCreate } from '@/api/enterpriseWeChat/link'
  import { doAccount } from '@/api/enterpriseWeChat/orderCode'
  export default defineComponent({
    name: 'TableEdit',
    emits: ['fetch-data'],
    setup(props, { emit }) {
      const $baseMessage = inject('$baseMessage')
      const state = reactive({
        formRef: null,
        form: {
          corp_id: null,
          link_name: '',
        },
        rules: {
          link_name: [
            { required: true, trigger: 'blur', message: '请输入链接名称' },
          ],
        },
        title: '',
        dialogFormVisible: false,
      })

      const showEdit = (corp_id) => {
        state.title = '创建获客链接'
        state.form.corp_id = corp_id
        state.dialogFormVisible = true
      }
      const close = () => {
        state['formRef'].resetFields()
        state.form = {}
        state.dialogFormVisible = false
      }
      const save = () => {
        state['formRef'].validate(async (valid) => {
          if (valid) {
            console.log(state.form)
            const { message } = await doLinkCreate(state.form)
            $baseMessage(message, 'success', 'vab-hey-message-success')
            emit('fetch-data')
            close()
          }
        })
      }
      onMounted(async () => {})
      return {
        ...toRefs(state),
        showEdit,
        close,
        save,
      }
    },
  })
</script>

<style lang="scss" scoped></style>
